package com.practice.controller;

import com.practice.annotation.LogSaver;
import com.practice.constants.Constants;
import com.practice.page.PageDto;
import com.practice.page.PageResult;
import com.practice.page.PageService;
import com.practice.pojo.dto.UserAddDto;
import com.practice.pojo.dto.UserLoginDto;
import com.practice.pojo.entity.User;
import com.practice.pojo.vo.UserVO;
import com.practice.service.UserService;
import com.practice.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * TODO your comment
 *
 * @author Masiyu
 * @date 2023/9/22 16:28
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@Slf4j
@Transactional
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PageService pageService;
    @PostMapping("/find-all")
    @ApiOperation("用户列表")
    public JsonResult<PageResult<User>> findAll(@RequestBody PageDto req){
        return new JsonResult<>(pageService.page(req,userService.findAll())) ;
    }
    
    @PostMapping("/add")
    @ApiOperation("新增用户")
    @LogSaver(value = "新增用户",operationType = Constants.OPERATION_TYPE_INSERT)
    public JsonResult<String> add(@RequestBody UserAddDto dto){
        User user = new User();
        BeanUtils.copyProperties(dto,user);
        userService.add(user);
        return new JsonResult<>();
    }
    
    @PostMapping("/login")
    @ApiOperation("登录")
    public JsonResult<String> login(@RequestBody @Valid UserLoginDto dto){
        log.debug("---------------/user/login-------------");
        String jwt = userService.login(dto.getName(), dto.getPassword(),dto.getPass());
        return new JsonResult<>(jwt);
    }
    
    @PostMapping("/get-current-user")
    @ApiOperation("获取当前用户信息")
    @LogSaver(value = "获取当前用户信息")
    public JsonResult<UserVO> getCurrentUser(){
        return new JsonResult<>(userService.getCurrentUser());
    }
    
}
